How do I [legally] get the current first responder on the screen on an iPhone?

Posted by Anthony D on Stack Overflow See other posts from Stack Overflow or by Anthony D
Published on 2009-12-01T00:31:14Z Indexed on 2010/05/22 10:20 UTC
Read the original article Hit count: 315

I submitted my app a little over a week ago and got the dreaded rejection email today. It reads as follows:

Dear -----------,

Thank you for submitting --------- to the App Store. Unfortunately it cannot be added to the App Store because it is using a private API. Use of non-public APIs, which as outlined in the iPhone Developer Program License Agreement section 3.3.1 is prohibited:

"3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs."

The non-public API that is included in your application is firstResponder.

Regards,

iPhone Developer Program

Now, the offending API call is actually a solution I found here on SO:

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView   *firstResponder = [keyWindow performSelector:@selector(firstResponder)];

So this is my question; How do I get the current first responder on the screen? I'm looking for a legal way that won't get my app rejected.

Thanks.

I figured this out based on the solution provided by Thomas below. Here is what the final code looks like:

@implementation UIView (FindFirstResponder)
- (UIView *)findFirstResonder
{
    if (self.isFirstResponder) {        
        return self;     
    }

    for (UIView *subView in self.subviews) {
    	UIView *firstResponder = [subView findFirstResonder];

        if (firstResponder != nil) {
    		return firstResponder;
    	}
    }

    return nil;
}
@end

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone-sdk